/*
 * Copyright (c) 2008-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.html;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JTextPane;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTML;

import org.panteleyev.blogclient.FontManager;

/**
 *
 * @author peterp
 */
public abstract class Editor {
    private JTextPane editor;
    private ArrayList<EditorEventListener> listeners = new ArrayList<>();

    public JTextPane getEditorPane() {
        return editor;
    }

    public void setEditor(JTextPane editor) {
        this.editor = editor;
        this.editor.setEditorKit(getKit());
    }

    public String getSelectedText() {
        return getEditorPane().getSelectedText();
    }

    public void insertText(String text) {
        getEditorPane().replaceSelection(text);
        getEditorPane().requestFocus();
    }

    public abstract EditorKit getKit();
    public abstract Document getDocument();
    public abstract String getHtml();

    public abstract void newDocument();

    public abstract void insertHTML(String html, HTML.Tag startTag);
    public abstract void insertUnicodeCharacter(char c);
    public abstract void encloseText(char open, char close);

    public abstract void setBold();
    public abstract void setItalic();
    public abstract void setUnderline();
    public abstract void setStrikeThrough();

    public abstract void setForegroundColor(Color color);

    public abstract void onInsertLink(HTMLLinkDialog dial);
    public abstract void onInsertImage(HTMLImageDialog dial);

    public abstract void setFormatTag(HTML.Tag tag);

    public abstract String getTextForPost();
    public abstract String getTextForPreview(boolean convertLineBreaks);

    public void fontUpdated() {
        editor.setFont(FontManager.getEditorFont());
    }

    public void addListener(EditorEventListener listener) {
        listeners.add(listener);
    }

    public void removeListener(EditorEventListener listener) {
        listeners.remove(listener);
    }

    protected ArrayList<EditorEventListener> getListeners() {
        return listeners;
    }

    /*
    public static final String getUrlFromClipboard() {
        try {
            String clipText = ClipboardUtil.getAsString();
            if ((clipText != null) &&
                 (clipText.startsWith("http://") || clipText.startsWith("https://"))
                 ) {
                return clipText;
            }
        } catch (Exception ex) {
        }

        return null;
    }
     */
}

